#include <QCoreApplication>
#include <management.h>
#include <QTime>
#include "common.h"



void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{

    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        fprintf(stderr, "%s\n", localMsg.constData());
        break;
    case QtInfoMsg:
        fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        abort();
    }
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString strMessage = QString("DateTime:%5   Message:%1   Function:%4   File:%2   Line:%3")
            .arg(msg).arg(context.file).arg(context.line).arg(context.function).arg(strDateTime);
    // 输出信息至文件中（读写、追加形式）
    if(Common::Log_Open)
    {
        Common::LogdataLock.lockForWrite();
        Common::Logdata.append(strMessage);
        Common::LogdataLock.unlock();
    }
    ///
    /// 没有启动日志会导致应用内存越来越大，因为Common::Logdata没有进行清除!!!
    ///
}

int main(int argc, char *argv[])
{
    Common::ConfigureLock.lockForRead();
    QString          Tcp_ip;                                     //连接tcp地址
    int              Tcp_port;                                   //tcp端口
    QString          LocalPort;                                  //本地监听端口
    int              Hand_Count;                                 //处理线程个数
    bool             Log_Open;                                   //日志开启
    Common::ConfigureLock.unlock();
    QCoreApplication a(argc, argv);
    qInstallMessageHandler(MessageOutput);
    Management *management=new Management();
    management->Configur_init("Caculate_node");
    management->StartConfigur();
    Common::ConfigureLock.lockForRead();
    Tcp_ip=Common::Tcp_ip;
    Tcp_port=Common::Tcp_port;
    LocalPort=Common::LocalPort;
    Hand_Count=Common::Hand_Count;
    Log_Open=Common::Log_Open;
    Common::ConfigureLock.unlock();
    if(Log_Open)
    {
        management->Log_init("Caculate_node");
        management->StartLog();
    }
    else
    {
        Common::LogdataLock.lockForWrite();
        Common::Logdata.clear();
        Common::LogdataLock.unlock();
        qInstallMessageHandler(NULL);
    }
    management->Handle_Setcount(Hand_Count);
    management->Handle_Start();
    management->localServer_init(LocalPort);
    management->localServer_start();
    management->tcp_init(Tcp_ip,Tcp_port);
    management->process_init();
    management->process_start();
    return a.exec();
}
